<?php

namespace app {

    use C\lib\Aes;
    use C\lib\Db;
    use C\lib\Session;
    use C\mod\user;
    use C\mod\worker;

    class ctr extends \C\ctr {
        
        public function getAesPost() {

            Db::connect();
            Session::start();

            if(isset($_SESSION['aes_key'])) {
                if (isset($_POST['aes'])) {
                    if ($aes = Aes::decrypt($_POST['aes'], $_SESSION['aes_key']))
                        if ($json = json_decode($aes, true)) {
                            if (isset($json['time'])) {
                                if ($json['time'] > $_SERVER['REQUEST_TIME'] - 600)
                                    return $json;
                                else {
                                    $this->writeAesJson(0, '请求超时');
                                }
                            } else {
                                $this->writeAesJson(0, '缺少 time 时间戳');
                            }
                        } else {
                            $this->writeAesJson(0, '您传输的加密内容异常');
                        }
                    else
                        $this->writeJson(0, 'Aes 解密失败');
                } else
                    $this->writeJson(0, '缺少 POST 的 aes 参数');
            } else
                $this->writeJson(-10, '缺失 Aes 密钥');
            return false;

        }


        public function getLogWorker() {
            if(isset($_SESSION['worker.id'])) {
                if($worker = worker::get(['id'=>$_SESSION['worker.id']]))
                    return $worker;
                else
                    $this->writeAesJson(0, '请先登录');
            } else
                $this->writeAesJson(0, '请先登录');
            return false;
        }

    }
    
}

